package com.nd.qa.shareplate.jdom;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class JDomParserDemo {

	public static void main(String[] args) {
		
		try {
			File file = new File("E:\\baiduyun\\parse.txt");
			//創建DocumentBuilder
			SAXBuilder saxBuilder = new SAXBuilder();
			//從文件或流創建一個文檔
			Document document = saxBuilder.build(file);
			System.out.println("Root element :" 
		            + document.getRootElement().getName());
			//提取根元素
			Element classElement = document.getRootElement();
			List<Element> studentElement = classElement.getChildren();
			for (Element student : studentElement) {
				//获取标签名称
				System.out.println("\nCurrent Element :" 
			               + student.getName());
				//获取标签内的属性名称
				Attribute attribute = student.getAttribute("rollno");
				//获取标签内属性名称--相对应--值
				 System.out.println("Student roll no : " 
			               + attribute.getValue() );
				 //获取student下的子元素
				 System.out.println("First Name : " + student.getChild("firstname").getText());
	            System.out.println("Last Name : "+ student.getChild("lastname").getText());
	            System.out.println("Nick Name : "+ student.getChild("nickname").getText());
	            System.out.println("Marks : "+ student.getChild("marks").getText());	    
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
}
